package com.example.ssm.controller;

import com.example.ssm.entity.*;
import com.example.ssm.service.BillService;
import com.example.ssm.service.MenuService;
import com.example.ssm.service.ShopService;
import com.example.ssm.service.UserService;
import com.example.ssm.service.impl.BillServiceImpl;
import com.example.ssm.service.impl.MenuServiceImpl;
import com.example.ssm.service.impl.ShopServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("/addUser")//注册用户
    public String addOrNo(User user, Model model) throws IOException {
        String a=null;

        User user1=userService.login(user);
        if(user1!=null){
            System.out.println("用户已存在");
            model.addAttribute("a","用户已存在");
            return "submit";
        }
        boolean flag = userService.submit(user);
        return flag ? "login":"submit";
    }

    @RequestMapping("/Login")//登录
    public String Login(User user, Model model, HttpServletRequest req) throws IOException {
        User flag = userService.login(user);
        List<UpComing_Shop> shop=new ArrayList<UpComing_Shop>();
         ShopService shopService=new ShopServiceImpl();
         shop =shopService.selectShop();

        //向页面封装响应数据
        if (flag==null) {
            System.out.println("账号为空，去注册");
            return "submit";
        } else {
            HttpSession session =req.getSession();
            session.setAttribute("user",user.getUname());
            if(flag.getUname().equals(user.getUname())&&flag.getPwd().equals(user.getPwd())){
                String i=flag.getPermission();
                if(i.equals("管理员")){
                    List<User> list=new ArrayList<User>();
                    list=userService.selectAllUser();
                    session.setAttribute("admin",flag);
                    model.addAttribute("Shop",shop);
                    System.out.println(shop);
                    req.setAttribute("list",shop);
                    model.addAttribute("User",list);
                    return "admin";
                }
                if(i.equals("商家")){
                    session.setAttribute("merchant",flag);
                    req.setAttribute("merchant",flag);

                    MenuService service=new MenuServiceImpl();
                    Menu menu=new Menu();
                    Merchant merchant1= shopService.select(flag.getUname());
                    List<Menu> menus=new ArrayList<Menu>();
                    menus=service.selectMenuByShopName(merchant1.getShop_name());
                    req.setAttribute("list",menus);
                    model.addAttribute("menus",menus);
                    session.setAttribute("merchant",flag);
                    Merchant merchant=new Merchant();
                    merchant= shopService.select(flag.getUname());

                    BillService billService=new BillServiceImpl();
                    List<Bill> NOPayBill= new ArrayList<Bill>();
                    NOPayBill=  billService.selectNoPayByshop_name(merchant);
                    System.out.println(flag);
                    List<Bill> PayedBill= new ArrayList<Bill>();
                    PayedBill= billService.selectPayByshop_name(merchant);
                    double value=0;
                    int j=0;
                    while (j<NOPayBill.size() ){
                        double price= NOPayBill.get(j).getPrice();
                        value+=price;
                        j++;
                    }
                    model.addAttribute("value",value);

                    double value1=0;
                    int k=0;
                    while (k<PayedBill.size() ){
                        double price= PayedBill.get(k).getPrice();
                        value1+=price;
                        k++;
                    }
                    model.addAttribute("value1",value1);
                    model.addAttribute("NOPayBill",NOPayBill);
                    model.addAttribute("PayedBill",PayedBill);
                    return "Merchantback";
                }
                if(i.equals("用户")){

                    session.setAttribute("USER",flag);
                    req.setAttribute("USER",flag);
                    MenuService menuService=new MenuServiceImpl();
                    BillService billService=new BillServiceImpl();

                    List<Menu>menus= menuService.selectAllMenu();
                    menus=menuService.selectAllMenu();
                    req.setAttribute("list",menus);
                    model.addAttribute("menus",menus);

                    List<Bill> NOPayBill= new ArrayList<Bill>();
                    NOPayBill=  billService.selectNoPayByUserName(user);
                    List<Bill> PayedBill= new ArrayList<Bill>();
                    PayedBill= billService.selectPayByUserName(user);
                    double value=0;
                    int j=0;
                    while (j<NOPayBill.size() ){
                        double price= NOPayBill.get(j).getPrice();
                        value+=price;
                        j++;
                    }
                    model.addAttribute("value",value);

                    double value1=0;
                    int k=0;
                    while (k<PayedBill.size() ){
                        double price= PayedBill.get(k).getPrice();
                        value1+=price;
                        k++;
                    }
                    model.addAttribute("value1",value1);
                    model.addAttribute("NOPayBill",NOPayBill);
                    model.addAttribute("PayedBill",PayedBill);
                    return "shophome";
                }
            }else {
                String a="密码错误!";
                model.addAttribute("a",a);
                System.out.println("密码错误,找回密码！");

            }
        }
return "login";
    }


    @RequestMapping("/recharge")
    public String recharge(Model model,String m,HttpServletRequest req) throws IOException {
        HttpSession session=req.getSession();
        String uname= (String) session.getAttribute("user");
        User user=new User();
        User user1;
        user.setUname(uname);

        double acount;
        user1=userService.selectByName(user);
        acount= Double.parseDouble(m)+user1.getAccount();
        user.setAccount(acount);
        userService.addMoney(user);
        User flag;
        flag= userService.selectByName(user);
        session.setAttribute("USER",flag);
        req.setAttribute("USER",flag);

        MenuService menuService=new MenuServiceImpl();
        BillService billService=new BillServiceImpl();

        List<Menu> menus= menuService.selectAllMenu();
        menus=menuService.selectAllMenu();
        req.setAttribute("list",menus);
        model.addAttribute("menus",menus);

        List<Bill> NOPayBill= new ArrayList<Bill>();
        NOPayBill=  billService.selectNoPayByUserName(user1);
        List<Bill> PayedBill= new ArrayList<Bill>();
        PayedBill= billService.selectPayByUserName(user1);
        double value=0;
        int j=0;
        while (j<NOPayBill.size() ){
            double price= NOPayBill.get(j).getPrice();
            value+=price;
            j++;
        }
        model.addAttribute("value",value);

        double value1=0;
        int k=0;
        while (k<PayedBill.size() ){
            double price= PayedBill.get(k).getPrice();
            value1+=price;
            k++;
        }
        model.addAttribute("value1",value1);
        model.addAttribute("NOPayBill",NOPayBill);
        model.addAttribute("PayedBill",PayedBill);

        return "shophome";
    }
}